<?php namespace App\Http\Requests;

use App\Services\ValidateService;
use Validator;
use Auth;
class AdminLoginRequest extends Request {

	/**
	 * Determine if the user is authorized to make this request.
	 *
	 * @return bool
	 */
	public function authorize()
	{
		return true;
	}

	/**
	 * Get the validation rules that apply to the request.
	 *
	 * @return array
	 */
	public function rules()
	{
		return [
			'name' => 'required',
			'password' => 'required',
            'checkcode' => 'required|verify_code',
		];
	}

	public function validator()
	{
        $msg = [
            'verify_code' => ':attribute 不正确'
        ];

		$nickName = [
			'name' => '管理员名字',
			'password' => '管理员密码',
            'checkcode' => '验证码'
		];

		$data = $this->only('name', 'password', 'checkcode');

        Validator::extend('verify_code', function($attribute, $value, $parameters) {
            return $value == \Session::get('code');
        });

		$validator = Validator::make($data, $this->rules(), $msg);

		$validator->setAttributeNames($nickName);

        if($validator->passes())
        {
            $validator->after(function ($validator) use ($data) {

                if (!Auth::attempt($this->only('name', 'password'), $this->has('remember'))) {
                    $validator->errors()->add('name', '管理员密码不正确');
                }
            });
        }

		return $validator;
	}
}
